package com.lds.sign_sdk.util;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

/**
 * @Date 2022/1/7
 * @Time 15:43
 * @Author liudongs
 * @E-mail liudongs@aliyun.com
 * @Version 1.0.0
 * @Description
 **/
public class Digest {
    private static final String EQ_SYMBOL = "=";
    private static final String AND_SYMBOL = "&";

    public Digest() {
    }

    public static String digest(String str) throws UnsupportedEncodingException {
        String md5 = null;
        md5 = MD5.digest(str, "UTF-8");
        return md5;
    }

    public static String digest(Map<String, String> map, List<String> excudeKeylist) throws UnsupportedEncodingException {
        TreeMap<String, String> treeMap = treeMap(map, excudeKeylist);
        return digest(mapToString(treeMap));
    }

    public static TreeMap<String, String> treeMap(Map<String, String> map, List<String> excudeKeylist) {
        TreeMap<String, String> treeMap = new TreeMap();
        treeMap.putAll(map);
        if (null != excudeKeylist && excudeKeylist.size() > 0) {
            Iterator var3 = excudeKeylist.iterator();

            while(var3.hasNext()) {
                String key = (String)var3.next();
                treeMap.remove(key);
            }
        }

        return treeMap;
    }

    public static String mapToString(Map<String, String> map) {
        StringBuilder result = new StringBuilder();
        Iterator var2 = map.entrySet().iterator();

        while(var2.hasNext()) {
            Entry<String, String> entry = (Entry)var2.next();
            String value = entry.getValue() == null ? "" : ((String)entry.getValue()).trim();
            result.append((String)entry.getKey()).append("=").append(value).append("&");
        }

        if (result.length() > 0) {
            result.deleteCharAt(result.length() - 1);
        }

        return result.toString().trim();
    }
}
